const db = require('../models');
class tokenService {
    //微信accesstoken查询
    async findAccessToken() {
        let resultToken = {}
        let result = {}
        try {
            resultToken = await db.wxtoken.findOne({
                attributes: ['AccessToken', 'AExpireTime'],
                where: {
                    APPID: 1
                },
                raw: true
            });
            result = { "access_token": resultToken['AccessToken'], "expires_in": resultToken['AExpireTime'] }
            return { code: 1, data: result }

        } catch (error) {
            return { code: 0, msg: 'AccessToken查询出错' }
        }
    };
    //更新accessToken
    async updataAccessToken(params) {
        if (params.errcode == null) {
            let { access_token, expires_in } = params;
            let date = new Date();
            expires_in = date.getTime() + 6900 * 1000;
            const updataResult = await db.wxtoken.update({ AccessToken: access_token, AExpireTime: expires_in }, {
                where: {
                    APPID: 1
                }
            })
            if (Number(updataResult) == 1) {
                return { "code": 1, msg: '数据库更新成功' }
            } else {
                return { "code": 0, msg: '数据库更失败' }
            }
        } else {
            return { "code": 0, msg: '数据库更失败' }
        }

    };
}
/**
 * 
测试代码
var token = new tokenService();
token.findAccessToken().then(value => {
    console.log(value);
})
 */
/**
 *var token = new tokenService();
token.updataAccessToken({ access_token: '1', expires_in: '7200' }).then(value => {
    console.log(value);
}) 
 */




module.exports = new tokenService();